package com.yjd.comm.util;

import org.apache.log4j.Logger;

import com.yjd.comm.annotation.ServiceRemoteProxyHandle;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.vo.Constant.PropertiesFileEnum;

/**
 * <b>description</b>：service服务获取工具 <br>
 * <b>time</b>：2014-8-22 下午7:14:54 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class ServiceHolder {
	private static Logger logger = Logger.getLogger(ServiceHolder.class);

	/**
	 * 获取service对象
	 * 
	 * @param serviceClass
	 * @param serviceName
	 * @return
	 */
	public static <T> T getService(Class<T> serviceClass, String serviceName) {
		return getService(serviceClass, serviceName, RpmServiceKeyEnum.RPM_PUBLIC_KEY_E);
	}
	
	/**
	* 获取service对象
	* @param serviceClass
	* @param serviceName
	* @param rpmServiceKeyEnum
	* @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getService(Class<T> serviceClass, String serviceName,RpmServiceKeyEnum rpmServiceKeyEnum) {
		Object object = null;
		try {
			object = SpringContextHolder.getBean(serviceName);
			if (object != null) {
				return (T) object;
			}
		} catch (Exception e) {
		}
		try {
			return (T) ServiceRemoteProxyHandle.getServiceProxy(serviceClass,
					rpmServiceKeyEnum.getRpmServiceUrl() + "/" + serviceName);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return null;
	}
	
	

	/**
	 * 获取service对象
	 * @param serviceClass
	 * @param serviceName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getServiceByUrl(Class<T> serviceClass,
			String serviceName, String url) {
		Object object = null;
		try {
			object = SpringContextHolder.getBean(serviceName);
			if (object != null) {
				return (T) object;
			}
		} catch (Exception e) {
		}
		try {
			return (T) ServiceRemoteProxyHandle.getServiceProxy(serviceClass,
					url + "/" + serviceName);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return null;
	}
}